package com.ydgk.controller;

import com.ydgk.bean.FurnitureKinds;
import com.ydgk.service.FurnitureKindsService;
import com.ydgk.tool.Tool;
import com.ydgk.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Map;

@RequestMapping("/furnitureKinds")
@Controller
public class FurnitureKindsController {
    @Autowired
    private FurnitureKindsService furnitureKindsService;

    private static final String UPLOAD_DIRECTORY = "upload/furnitureKinds";
    public static MultipartFile fileForFkface = null;

    @ModelAttribute
    public void getFurnitureKinds(@RequestParam(value = "fkid" , required = false , defaultValue = "0") Integer fkid, Map map){
        if(fkid != 0){
            FurnitureKinds furnitureKinds = furnitureKindsService.findById(fkid);
            map.put("furnitureKinds",furnitureKinds);
        }
    }

    @ResponseBody
    @RequestMapping("/fkfaceUpload")
    public AjaxResult fkfaceUpload(@RequestParam("file") MultipartFile file) throws IOException {
        this.fileForFkface = file;
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/insertFurnitureKinds")
    public AjaxResult insertFurnitureKinds(FurnitureKinds furnitureKinds, HttpServletRequest request) throws IOException {
        String fkface = Tool.upload(request, fileForFkface , UPLOAD_DIRECTORY);
        furnitureKinds.setFkface(UPLOAD_DIRECTORY + "/" + fkface);
        int i = furnitureKindsService.insert(furnitureKinds);
        AjaxResult ajaxResult = new AjaxResult();
        if(i > 0){
            ajaxResult.setSuccess(true);
            clear();
        }else{
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/updateInfo")
    public AjaxResult updateInfo(FurnitureKinds furnitureKinds) {
        AjaxResult ajaxResult = new AjaxResult();
        int i = furnitureKindsService.updateById(furnitureKinds);
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/updateFace")
    public AjaxResult updateFace(FurnitureKinds furnitureKinds , HttpServletRequest request) throws IOException {
        ServletContext application = request.getServletContext();
        String newfilename = Tool.upload(request,fileForFkface,UPLOAD_DIRECTORY );
        String fkface = furnitureKinds.getFkface();
        furnitureKinds.setFkface(UPLOAD_DIRECTORY + "/" + newfilename);
        int i = furnitureKindsService.updateById(furnitureKinds);
        AjaxResult ajaxResult = new AjaxResult();
        if( i > 0){
            new File(application.getRealPath("/"+fkface)).delete();
            clear();
            ajaxResult.setSuccess(true);
        }else{
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/deleteFurnitureKinds")
    public AjaxResult deleteFurnitureKinds(Integer fkid) {
        AjaxResult ajaxResult = new AjaxResult();
        int i = furnitureKindsService.deleteById(fkid);
        if(i > 0){
            ajaxResult.setSuccess(true);
        }else {
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

    public static void clear(){
        fileForFkface = null;
    }
}
